#include "apdu_mgr_base.h"

#include <stdlib.h>
#include <string.h>

#include "../apdu.algo/sm4.h"

apdu_mgr_base::apdu_mgr_base(void)
{
}

apdu_mgr_base::~apdu_mgr_base(void)
{
}


unsigned char* apdu_mgr_base::sm_encrypt(unsigned char key[16], unsigned char * input, int input_len, int * out_len )
{
	int pad_len = 16;
	int new_len = 0;
	unsigned char * out_buff = NULL;

	new_len = input_len + 2; // fill 2 bytes length
	new_len = ((new_len / pad_len) + 1) * pad_len; //get final length

	out_buff = (unsigned char*)malloc(new_len);
	memset(out_buff,0, new_len);

	int pos = 0;
	memcpy(out_buff, &input_len, 2);
	pos += 2;

	memcpy(out_buff + pos, input, input_len);
	pos += input_len;

	out_buff[pos] = 0x80;

	sm4_ecb(key, out_buff, new_len, SM4_ENCRYPT);

	* out_len = new_len;

	return out_buff;
}

apdu * apdu_mgr_base::create_apdu_transmit_more_data( apdu * main_apdu, unsigned char * data, int data_len)
{
	apdu * newApdu = main_apdu->clone();
	newApdu->set_lc_data(data, data_len);

	return newApdu;
}

